import { gameConfig, gameConfigHandle } from "js/config/gameConfig"
import Random from "js/core/random"
import utils from "js/core/utils"
import { ItemModel } from "js/model/itemModel"
import { PlayerModel } from "js/model/playerModel"
import equipmentHandle from "./equipmentHandle"
import itemHandle from "./itemHandle"
import playerHandle from "./playerHandle"
import treasureHandle from "./treasureHandle"
import gameUtils from "js/core/gameUtils"

class MarketHandle {
    getRefreshCostOfNormalMarket(player: PlayerModel, time: number) {
        let marketTimesOfAfd = 0
        if (playerHandle.isMember(player, time)) {
            marketTimesOfAfd = gameConfigHandle.gameConfig.vip.marketTimesOfAfd
        }

        let refreshedTimes = player.market.refreshTimes
        let vip = playerHandle.getVipLvl(player)
        let vipTimes = 0
        if (vip) {
           vipTimes += (gameConfig.vip.addMarketTimes[vip] || 0)
        }

        if (player.baseInfo.longVip) {
            vipTimes += gameConfig.vip.marketTimesOfLongVip
         }
        // let freeRefreshTimesMax = utils.getIncValue(gameConfigHandle.gameConfig.market.freeRefreshTimesMax, vip) + marketTimesOfVip
        let freeRefreshTimesMax = gameConfigHandle.gameConfig.market.freeRefreshTimes + vipTimes + marketTimesOfAfd
        let cost = 0
        let freeTimes = 0
        if (freeRefreshTimesMax > refreshedTimes) {
            freeTimes = freeRefreshTimesMax - refreshedTimes
        } else {
            let num = refreshedTimes - freeRefreshTimesMax
            let costJs = gameConfigHandle.gameConfig.market.refreshCost
            let costMax = gameConfigHandle.gameConfig.market.refreshCostMax
            cost = utils.getIncValue(costJs, num)
            if (cost > costMax) {
                cost = costMax
            }
        }

        return { cost, freeTimes }
    }

    getMarketItems(player: PlayerModel, rand: Random) {
        let items: ItemModel[] = []
        let costs: any[] = []
        let { mapID, floor } = gameUtils.getMapMax(player.baseInfo.mapProgressMax)
        let elvl = gameConfigHandle.configByID[mapID].mapLvl + player.adventureInfo.floor - 1
        let latticeAmount = gameConfigHandle.gameConfig.market.latticeAmount
        let coe = gameConfigHandle.gameConfig.market.coe
        let discountConfig = gameConfigHandle.gameConfig.market.discount
        let chance = gameConfigHandle.gameConfig.market.chance
        //随机生成物品
        for (let i = 0; i < latticeAmount; i++) {
            let sjs = rand.intn(coe)
            let item: ItemModel
            let cost
            if (sjs <= chance[0]) {//装备
                let equipmentChance = gameConfigHandle.gameConfig.market.equipmentChance
                let quality = utils.getRandomValue(equipmentChance, coe, rand)
                if (quality >= 4) {
                    quality = 4
                }
                item = equipmentHandle.create(player, null, elvl, rand, quality)
                let gold = equipmentHandle.getMoneyPriceOfEquipment(item) * 5
                let diamond = equipmentHandle.getGoldPriceOfEquipment(item) * 5
                if (diamond <= 0) {
                    cost = { gold }
                } else {
                    cost = { diamond }
                }
            } else if (sjs <= chance[1]) {//基础材料
                let itemIDs = gameConfigHandle.gameConfig.base.normalItems
                let tID = utils.getOneFromArray(itemIDs, rand)
                let resourceAmount = gameConfigHandle.gameConfig.market.price[tID].resourceAmount
                let mapConfig = gameConfigHandle.configByID[mapID]
                let floorLvl = mapConfig.mapLvl + floor - 1
                let stack = utils.getIncValue(gameConfigHandle.gameConfig.map.getResource, floorLvl)
                    * resourceAmount
                item = itemHandle.create({ tID, stack }, rand)
                let costPer = gameConfigHandle.gameConfig.market.price[tID].costPer
                let costSj = rand.intn(coe)
                let costConfig = gameConfigHandle.gameConfig.market.price[tID].cost
                cost = (costSj <= costPer[0]) ? { diamond: costConfig.diamond } : { gold: costConfig.gold }
            } else if (sjs <= chance[3]) {//稀有材料
                let itemIDs = gameConfigHandle.gameConfig.base.rareItems
                let tID = utils.getOneFromArray(itemIDs, rand)
                let amount = gameConfigHandle.gameConfig.market.price[tID].amount
                let stack = utils.getOneFromArray(amount, rand)
                item = itemHandle.create({ tID, stack }, rand)
                let costPer = gameConfigHandle.gameConfig.market.price[tID].costPer
                let costSj = rand.intn(coe)
                let costConfig = gameConfigHandle.gameConfig.market.price[tID].cost
                cost = (costSj <= costPer[0]) ? { diamond: costConfig.diamond } : { gold: costConfig.gold }
            } else if (sjs <= chance[4]) {//宝物
                let quaility = rand.intn(4)
                let treasureIDs = gameConfigHandle.gameConfig.base.treasureList
                let sjIndex = rand.intn(treasureIDs.length) - 1
                let tid = treasureIDs[sjIndex]
                item = treasureHandle.shopCreate(tid, 1, rand, quaility)
                let diamondPrice = utils.getIncValue(gameConfigHandle.gameConfig.market.treasureDiamondCost, item.treasure.quality)
                if (item.treasure.quality > 1) {
                    cost = { diamond: diamondPrice }
                } else {
                    cost = { gold: diamondPrice * 1000 }
                }
            } else if (sjs <= chance[5]) {
                let itemIDs = gameConfigHandle.gameConfig.base.gemList
                let tID = utils.getOneFromArray(itemIDs, rand)
                let gemChance = gameConfigHandle.gameConfig.market.gemChance
                let gemAmount = gameConfigHandle.gameConfig.market.gemAmount
                let stack = utils.getOneFromArray(gemAmount, rand)
                //随机宝石等级
                let sj = rand.intn(coe)
                let hp = 1
                if (sj <= gemChance[0]) {
                    hp = 1
                } else if (sj <= gemChance[1]) {
                    hp = 2
                } else if (sj <= gemChance[2]) {
                    hp = 3
                }
                item = itemHandle.create({ tID, stack: (stack || 1), hp }, rand)
                let gemCostPer = gameConfigHandle.gameConfig.market.gemCostPer
                let gemPriceConfig = gameConfigHandle.gameConfig.market.gemPrice
                let gemUpNeedAmount = gameConfigHandle.gameConfig.gem.gemUpNeedAmount
                let gemSj = rand.intn(coe)
                if (gemSj <= gemCostPer[0]) {
                    let price = gemPriceConfig.diamond * Math.pow(gemUpNeedAmount, hp - 1)
                    cost = { diamond: price }
                } else {
                    let price = gemPriceConfig.gold * Math.pow(gemUpNeedAmount, hp - 1)
                    cost = { gold: price }
                }
            } else {
                let itemIDs = gameConfigHandle.gameConfig.base.gemList
                let tID = utils.getOneFromArray(itemIDs, rand)
                let gemChance = gameConfigHandle.gameConfig.market.gemChance
                let gemAmount = gameConfigHandle.gameConfig.market.gemAmount
                let stack = utils.getOneFromArray(gemAmount, rand)
                //随机宝石等级
                let sj = rand.intn(coe)
                let hp = 0
                if (sj <= gemChance[0]) {
                    hp = 1
                } else if (sj <= gemChance[1]) {
                    hp = 2
                } else if (sj <= gemChance[2]) {
                    hp = 3
                } else {
                    hp = 4
                }
                item = itemHandle.create({ tID, stack, hp }, rand)
                let gemCostPer = gameConfigHandle.gameConfig.market.gemCostPer
                let gemPriceConfig = gameConfigHandle.gameConfig.market.gemPrice
                let gemUpNeedAmount = gameConfigHandle.gameConfig.gem.gemUpNeedAmount
                let gemSj = rand.intn(coe)
                if (gemSj <= gemCostPer[0]) {
                    let price = gemPriceConfig.diamond * Math.pow(gemUpNeedAmount, hp - 1)
                    cost = { diamond: price }
                } else {
                    let price = gemPriceConfig.gold * Math.pow(gemUpNeedAmount, hp - 1)
                    cost = { gold: price }
                }

            }
            let discount = utils.getOneFromArray(discountConfig, rand)
            cost['discount'] = discount
            costs.push(cost)
            items.push(item)
        }
        return { items, costs }
    }
    haveFreeTimes(player: PlayerModel, time: number) {
        let refreshTimes = player.market.refreshTimes
        let vip = playerHandle.getVipLvl(player)
        let freeTimesMax = gameConfigHandle.gameConfig.market.freeRefreshTimesMax[vip]
        if (playerHandle.isMember(player, time)) {
            freeTimesMax += gameConfigHandle.gameConfig.vip.marketTimesOfAfd
        }
        if (refreshTimes >= freeTimesMax) {
            return false
        }
        return true
    }
}

let marketHandle = new MarketHandle()
export default marketHandle